package com.xiaofu.controller;


import com.xiaofu.enums.BizCodeEnum;
import com.xiaofu.request.UserLoginRequest;
import com.xiaofu.request.UserRegisterRequest;
import com.xiaofu.service.AddressService;
import com.xiaofu.service.FileService;
import com.xiaofu.service.UserService;
import com.xiaofu.utils.JsonData;
import com.xiaofu.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小夫
 * @since 2024-07-01
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     * 上传用户头像
     * @param file 默认最大为1M,超过则报错
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("用户头像上传")
    public JsonData uploadUserImg(@ApiParam(value = "文件上传",required = true)
                                      @RequestPart("file") MultipartFile file) {
        String filePath = fileService.uploadUserImg(file);
        return filePath!=null ?JsonData.buildSuccess(filePath) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    @ApiOperation("用户注册")
    @PostMapping("register")
    public JsonData register(@ApiParam("用户注册对象")@RequestBody UserRegisterRequest userRequest) {

        return userService.register(userRequest);
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(@ApiParam("用户登录对象")@RequestBody UserLoginRequest request) {
        return userService.login(request);
    }

    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail() {
        UserVO userVO = userService.findUserDetail();
        return JsonData.buildSuccess(userVO);
    }

}

